//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SmileSection.h"
using namespace Cephei::QL::Termstructures::Volatility;
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (boost::shared_ptr<QuantLib::SmileSection>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSmileSection = NULL;
#endif
	_ppSmileSection = &childNative;
    
}
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (QuantLib::SmileSection& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSmileSection = NULL;
#endif
	_ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (&childNative);
    
    _SmileSectionOwner = owner;
    
}

Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (CSmileSection^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSmileSection = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSmileSection = NULL;
#endif
	if (!t->IsSubclassOf(CSmileSection::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (QuantLib::Handle<QuantLib::SmileSection>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSmileSection = &childNative;
	_ppSmileSection = &static_cast<boost::shared_ptr<QuantLib::SmileSection>>(childNative.currentLink());
    
    _SmileSectionOwner = owner;
}
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (QuantLib::Handle<QuantLib::SmileSection> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSmileSection = &childNative;
	_ppSmileSection = &static_cast<boost::shared_ptr<QuantLib::SmileSection>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::CSmileSection::CSmileSection (QuantLib::SmileSection childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSmileSection = NULL;
#endif
	_ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (new QuantLib::SmileSection (childNative));
    
}
#endif

Cephei::QL::Termstructures::Volatility::CSmileSection::~CSmileSection ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSmileSection != NULL)
    {
	    delete _ppSmileSection;
        _ppSmileSection = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::CSmileSection::!CSmileSection ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSmileSection != NULL)
    {
	    delete _ppSmileSection;
    }
}
QuantLib::SmileSection& Cephei::QL::Termstructures::Volatility::CSmileSection::GetReference ()
{
    if (_ppSmileSection == NULL) throw REFNEW NativeNullException ();
	return **_ppSmileSection;
}
boost::shared_ptr<QuantLib::SmileSection>& Cephei::QL::Termstructures::Volatility::CSmileSection::GetShared ()
{
    if (_ppSmileSection == NULL) throw REFNEW NativeNullException ();
	return *_ppSmileSection;
}
QuantLib::SmileSection* Cephei::QL::Termstructures::Volatility::CSmileSection::GetPointer ()
{
    if (_ppSmileSection == NULL) throw REFNEW NativeNullException ();
	return &**_ppSmileSection;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SmileSection>& Cephei::QL::Termstructures::Volatility::CSmileSection::GetHandle ()
{
	if (_phSmileSection == NULL)
	{
		_phSmileSection = new Handle<QuantLib::SmileSection> (*_ppSmileSection);
	}
	return *_phSmileSection;
}
#endif
bool Cephei::QL::Termstructures::Volatility::CSmileSection::HasNative () 
{
	return (_ppSmileSection != NULL);
}

Double Cephei::QL::Termstructures::Volatility::CSmileSection::AtmLevel::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSmileSection)->atmLevel ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::CSmileSection::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter& _rv = (QuantLib::DayCounter&)(*_ppSmileSection)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::CSmileSection::ExerciseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSmileSection)->exerciseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSmileSection::ExerciseTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppSmileSection)->exerciseTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSmileSection::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSmileSection)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSmileSection::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSmileSection)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::CSmileSection::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSmileSection)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSmileSection::Variance (Double strike)
{
    try
    {
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSmileSection)->variance ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSmileSection::Volatility (Double strike)
{
    try
    {
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppSmileSection)->volatility ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::CSmileSection::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSmileSection)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

